package com.canxi.games.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.canxi.games.domain.bo.CxGamesBo;
import com.canxi.games.domain.vo.CxGamesVo;
import com.canxi.games.domain.CxGames;
import com.canxi.games.mapper.CxGamesMapper;
import com.canxi.games.service.ICxGamesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.canxi.cxcommon.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.util.StrUtil;

/**
 * 游戏列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-20
 */
@RequiredArgsConstructor
@Service
public class CxGamesServiceImpl  extends JoinServiceImpl<CxGamesMapper, CxGames> implements ICxGamesService {

    private final CxGamesMapper baseMapper;

    @Override
    public TableDataInfo<CxGamesVo> queryCxGamesList(CxGamesBo cxGamesBo) {


        return PageUtils.buildDataInfo(this.baseMapper.queryCxGamesList(PageUtils.buildPage(), BeanUtil.toBean(cxGamesBo, CxGamesVo.class)));
    }

    @Override
    public List<CxGamesVo> queryCxGamesAll(CxGamesBo cxGamesBo) {
        return this.baseMapper.queryCxGamesList(BeanUtil.toBean(cxGamesBo, CxGamesVo.class));
    }



    /**
     * 查询游戏列表
     */
    @Override
    public CxGamesVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询游戏列表列表
     */
    @Override
    public TableDataInfo<CxGamesVo> queryPageList(CxGamesBo bo, PageQuery pageQuery) {
        return PageUtils.buildDataInfo(this.baseMapper.queryCxGamesList(PageUtils.buildPage(), BeanUtil.toBean(bo, CxGamesVo.class)));
    }

    /**
     * 查询游戏列表列表
     */
    @Override
    public List<CxGamesVo> queryList(CxGamesBo bo) {
        LambdaQueryWrapper<CxGames> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CxGames> buildQueryWrapper(CxGamesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CxGames> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getTenantId()), CxGames::getTenantId, bo.getTenantId());
        lqw.like(StrUtil.isNotBlank(bo.getName()), CxGames::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getNameAlias()), CxGames::getNameAlias, bo.getNameAlias());
        lqw.eq(StrUtil.isNotBlank(bo.getGameDesc()), CxGames::getGameDesc, bo.getGameDesc());
        lqw.eq(StrUtil.isNotBlank(bo.getImgs()), CxGames::getImgs, bo.getImgs());
        lqw.eq(StrUtil.isNotBlank(bo.getVideo()), CxGames::getVideo, bo.getVideo());
        return lqw;
    }

    /**
     * 新增游戏列表
     */
    @Override
    public Boolean insertByBo(CxGamesBo bo) {
        CxGames add = BeanUtil.toBean(bo, CxGames.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改游戏列表
     */
    @Override
    public Boolean updateByBo(CxGamesBo bo) {
        CxGames update = BeanUtil.toBean(bo, CxGames.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxGames entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除游戏列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
